/**
 * 
 */
/***************************************************************
                           病人管理模块
***************************************************************/
var addBloodDialog;
function bloodManagerInit()
{
	$("#bloodTable tr:nth-child(even)").addClass("trevencolor");
	  BUI.use(['bui/overlay','bui/form'],function(Overlay,Form){
	      	 var form = new Form.HForm({
	        	 srcNode : '#form'
   	  }).render();
	 
	  addBloodDialog = new Overlay.Dialog({
          title:'添加血液标本',
          width:740,
          height:480,
          //配置DOM容器的编号
          contentId:'blood_content',
          cancel:function(){
	      	 choose_close();
	      	 clearBloodDialog();
	      },
          success:function () {
        	  saveOrUpdateBlood();
            }
        });
    });
	 
	 
    $("#add_Blood").bind("click",function(){
	    clearBloodDialog();
//	    $("#blood_g_type").val("血清");
	    cyf_true();
		cea_true();
		active_false();
	    addBloodDialog.set("title","添加血液标本");
	    addBloodDialog.show();
	    choose_open();
     });
   
    $("#blood_startDate").css({cursor: 'pointer'});//手型光标
    $("#blood_endDate").css({cursor: 'pointer'});
    $("#blood_g_date").css({cursor: 'pointer'});
    $("#blood_startDate").click(function(){
		WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'});
    });
    $("#blood_endDate").click(function(){
		WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'});
    });
    $("#blood_g_date").click(function(){
		WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'});
    });
    
    var extractTimeUrl = projectName + "/blood/getExtractTimeList.do?date=" + new Date().getTime();
	$.ajax({
		url: extractTimeUrl,
		async: false,
		success: function(data,result){
			for(var i = 0; i < data.length; i++)
			{
				$("#blood_type").append("<option>"+data[i]+"</option>");
			}
		}
	});
	typeSelectChanged();
	
	var bloodTypeUrl = projectName + "/blood/getBloodTypeList.do?date=" + new Date().getTime();
	$.ajax({
		url: bloodTypeUrl,
		async: false,
		success: function(data,result){
			for(var i = 0; i < data.length; i++)
			{
				$("#blood_g_type").append("<option>"+data[i]+"</option>");
			}
		}
	});
	
   bloodValidate();
}
/**
 *  血液种类选中事件
 */
function typeSelectChanged()
{
	$("#blood_g_type").change(function(){
		var type = $("#blood_g_type").val();
		if("血清" == type)
		{
			cyf_true();
			cea_true();
			active_false();
		}
		else if("淋巴细胞" == type)
		{
			cyf_false();
			cea_false();
			active_true();
		}
		else
		{
			cyf_false();
			cea_false();
			active_false();
		}
	});
}

/**
 *  CYF 可填写
 */
function cyf_true()
{
	$("#cyf_0").attr("readonly",false);
	$("#cyf_0").css("background-color","");
	$("#cyf_6").attr("readonly",false);
	$("#cyf_6").css("background-color","");
	$("#cyf_10").attr("readonly",false);
	$("#cyf_10").css("background-color","");
}

/**
 *  CYF 不可填写
 */
function cyf_false()
{
	$("#cyf_0").attr("readonly","readonly");
	$("#cyf_0").css("background-color","#F0F0F0");
	$("#cyf_6").attr("readonly","readonly");
	$("#cyf_6").css("background-color","#F0F0F0");
	$("#cyf_10").attr("readonly","readonly");
	$("#cyf_10").css("background-color","#F0F0F0");
}

/**
 *  CEA 可填写
 */
function cea_true()
{
	$("#cea_0").attr("readonly",false);
	$("#cea_0").css("background-color","");
	$("#cea_6").attr("readonly",false);
	$("#cea_6").css("background-color","");
	$("#cea_10").attr("readonly",false);
	$("#cea_10").css("background-color","");
}

/**
 *  CEA 不可填写
 */
function cea_false()
{
	$("#cea_0").attr("readonly","readonly");
	$("#cea_0").css("background-color","#F0F0F0");
	$("#cea_6").attr("readonly","readonly");
	$("#cea_6").css("background-color","#F0F0F0");
	$("#cea_10").attr("readonly","readonly");
	$("#cea_10").css("background-color","#F0F0F0");
}

/**
 *  active 可填写
 */
function active_true()
{
	$("#active_0").attr("readonly",false);
	$("#active_0").css("background-color","");
	$("#active_6").attr("readonly",false);
	$("#active_6").css("background-color","");
	$("#active_10").attr("readonly",false);
	$("#active_10").css("background-color","");
}

/**
 *  active 不可填写
 */
function active_false()
{
	$("#active_0").attr("readonly","readonly");
	$("#active_0").css("background-color","#F0F0F0");
	$("#active_6").attr("readonly","readonly");
	$("#active_6").css("background-color","#F0F0F0");
	$("#active_10").attr("readonly","readonly");
	$("#active_10").css("background-color","#F0F0F0");
}

/**
 *	添加添加血液标本  id为空;
 *  编辑添加血液标本 
 */
function saveOrUpdateBlood()
{
    var id = $("#blood_id").val();
	if($("#bloodForm").validate().form())
	{
		addBloodDialog.close();
		var url = projectName + "/blood/saveOrUpdate.do?postDate=" + new Date().getTime();
		$.ajax({
			url: url,
			type: "post",
			async: false,
			data: $('#bloodForm').serialize(),
			 error: function(XMLHttpRequest, textStatus, errorThrown) {
				 alert(XMLHttpRequest.status);
				 alert(XMLHttpRequest.readyState);
				 alert(textStatus);
				   },
			success: function(data,result)
			{
				if(id == "" || id == null)
				{
					if("ERROR" == data)
					{
						messageAlert(1,"添加血液标本失败，请重试",function()
								{
							this.close();
							choose_close();
							showBloodPageDataList(1);
						});
						
					}
					else
					{
						messageAlert(1,"添加血液标本成功"+data,function()
								{
									this.close();
									choose_close();
									showBloodPageDataList(1);
						});
					}
				}
				else
				{
					if("ERROR" == data){
						messageAlert(1,"修改血液标本失败，请重试",function()
								{
									this.close();
									choose_close();
									showBloodPageDataList(1);
						});
					}
					else
					{
						messageAlert(1,"修改血液标本成功",function()
								{
									this.close();
									choose_close();
									showBloodPageDataList(1);
						});
					}
					
				}
				
			}
		});
		
	}
}

/**
 *	清空表单数据 
 */
function clearBloodDialog(){
	bloodFormValidate.resetForm();
	$("#blood_mrn").val("");
	$("#blood_amount").val("");
	$("#blood_type").val("手术");
	$("#blood_g_type").val("血清");
	$("#blood_cyf_0").val("");
	$("#blood_cyf_6").val("");
	$("#blood_cyf_10").val("");
	$("#blood_cea_0").val("");
	$("#blood_cea_6").val("");
	$("#blood_cea_10").val("");
	$("#blood_active_0").val("");
	$("#blood_active_6").val("");
	$("#blood_active_10").val("");
	$("#blood_g_date").val("");
	$("#blood_memo").val("");
	$("#blood_id").val("");
}


/**
 *	删除血液标本 
 */
function deleteBlood(id)
{
	$.ajax({
		 url: projectName+"/blood/deleteByIds.do?date=" + new Date().getTime(),
		 type: "post",
		 data: {"ids": id},
		 async: false,
		 success: function(data,result)
		 {
			 if("SUCCESS" == data){
				 messageAlert(1,"删除血液标本成功",function()
				 {
						this.close();
						choose_close();
						showBloodPageDataList(1);
				 });
			 }
			 else if("ERROR" == data)
			 {
				 messageAlert(3,"删除血液标本失败",function(){
						this.close();
						choose_close();
						showBloodPageDataList(1);
				 });
			 }
		 }
	 });
}

/**
 *	判断批量删除血液标本是否为空
 */
function del_blood(cbxName)
{
	var ids = getSelectCbx(cbxName);
	if(ids == "")
	{
		messageAlert(2,"请选择要删除的血液标本！",function(){choose_close(),this.close()});
	}
	else
	{
		messageAlert(4,'你确定要删除选择的血液标本吗？',function(){choose_open,deleteByIds(ids)},1)
	}
}

/**
 *	批量删除血液标本 
 */
function deleteByIds(ids){
	$.ajax({
		 url : projectName + "/blood/deleteByIds.do?date=" + new Date().getTime(),
		 type: "post",
		 data: {"ids": ids},
		 async: false,
		 success: function(data,result){
			 if("SUCCESS" == data){
				 messageAlert(1,"删除血液标本成功",function(){
						this.close();
						choose_close();
						showBloodPageDataList(1);
				 });
			 }
			 
			 else if("ERROR" == data)
			 {
				 messageAlert(3,"删除血液标本失败",function(){
						this.close();
						choose_close();
						showBloodPageDataList(1);
				 });
			 }
		 }
	 });
}

/**
 *	编辑血液标本 
 */
function bloodEdit(id)
{
	$.ajax({
		url : projectName+"/blood/getBloodById.do?date=" + new Date().getTime(),
		async : false,
		data:{"id":id},
		success : function(data) {
			if(null != data&&data != "")
			{
				clearBloodDialog();
				$("#blood_mrn").val(data.mrn);
				$("#blood_amount").val(data.amount);
				$("#blood_type").val(data.type);
				$("#blood_g_type").val(data.g_type);
				$("#cyf_0").val(data.cyf_0);
				$("#cyf_6").val(data.cyf_6);
				$("#cyf_10").val(data.cyf_10);
				$("#cea_0").val(data.cea_0);
				$("#cea_6").val(data.cea_6);
				$("#cea_10").val(data.cea_10);
				$("#active_0").val(data.active_0);
				$("#active_6").val(data.active_6);
				$("#active_10").val(data.active_10);
				$("#blood_g_date").val(data.g_dateString);
				$("#blood_memo").val(data.memo);
				$("#blood_id").val(data.id);
				choose_open();
				addBloodDialog.set("title","编辑血液标本");
				addBloodDialog.show();		
			}
			else
			{
				messageAlert(0,"该血液标本已被删除！",function(){
					choose_close(),
					this.close(),
					showBloodPageDataList(1)
					});
			}
		}
	});
	
}


/***************************************************************
							血液标本分页
***************************************************************/
/**
 *	血液标本分页对象 
 */
var bloodPageInfo={
		orderBy:null,
		order:"desc",
		pageNo:"1",
};


/**
 * 查询血液标本数据
 */
function showBloodPageDataList(pageNo)
{
	bloodPageInfo.pageNo = pageNo;
	var search_mrn = getSearch_mrn("blood");
	var startDate = $("#blood_startDate").val();
	if(undefined == startDate||null == startDate)
	{
		startDate = "";
	}
	var endDate = $("#blood_endDate").val();
	if(undefined == endDate||null == endDate)
	{
		endDate = "";
	}
	var bloodPageInfoString = JSON.stringify(bloodPageInfo);
	window.location.href = projectName + "/blood/getPageBlood.do?PageInfo=" + bloodPageInfoString + "&search_mrn=" + search_mrn + "&startDate=" + startDate +"&endDate=" + endDate;
}


/***************************************************************
							表单验证
***************************************************************/
var bloodFormValidate;
function bloodValidate()
{
	bloodFormValidate = $("#bloodForm").validate({
		errorPlacement: function(error, element) { 
         	error.appendTo(element.next());
         },
		rules:{
			mrn:
			{
				required:true,
				theTrueMrn:true
			},
			amount:
			{
				required:true,
				number:true
			},
			cyf_0:
			{
				number:true,
			},
			cyf_6:
			{
				number:true,
			},
			cyf_10:
			{
				number:true,
			},
			cea_0:
			{
				number:true,
			},
			cea_6:
			{
				number:true,
			},
			cea_10:
			{
				number:true,
			},
			active_0:
			{
				number:true,
			},
			active_6:
			{
				number:true,
			},
			active_10:
			{
				number:true,
			},
			g_date_String:
			{
				required:true,
			}
		},
		messages:{
			mrn:
			{
                required:"患者病历号不能为空",
            },
            g_date_String:
			{
				required:"不能为空",
			},
        }
		
	});
}







